<template>
    <div class="app-container">
      <el-row :gutter="20">
        <!--地震速报-->
        <el-col :span="24" :xs="24">
          <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
            <el-form-item label="发震时刻">
              <el-date-picker
                v-model="dateRange"
                size="small"
                style="width: 240px"
                value-format="yyyy-MM-dd"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              ></el-date-picker>
            </el-form-item>
            <el-form-item label="状态" prop="status">
              <el-select
                v-model="queryParams.status"
                placeholder="状态"
                clearable
                size="small"
                style="width: 240px"
              >
                <el-option
                  v-for="dict in statusOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>

            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
<!--              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>-->
            </el-form-item>
          </el-form>
          <el-row :gutter="10" class="mb8">
            <el-table size="mini" v-loading="loading" :data="catalogList">
              <el-table-column label="发震时刻" align="center" prop="otime" v-if="columns[0].visible" />
              <el-table-column label="事件ID" align="center" key="scid" prop="scid" v-if="columns[1].visible" :show-overflow-tooltip="true" />
              <el-table-column label="纬度" align="center" key="lat" prop="lat" v-if="columns[2].visible" width="60" >
                <template slot-scope="scope">
                  <span>{{fun(scope.row.lat)}}</span>
                </template>
              </el-table-column>
              <el-table-column label="经度" align="center" key="lon" prop="lon"  v-if="columns[3].visible" width="60" >
                <template slot-scope="scope">
                  <span>{{fun(scope.row.lon)}}</span>
                </template>
              </el-table-column>
              <el-table-column label="深度" align="center" key="depth" prop="depth" v-if="columns[4].visible" width="60" />
              <el-table-column label="震级" align="center" key="m" prop="m" v-if="columns[5].visible" width="60"/>
              <el-table-column label="参考地名" align="center" prop="locationCname" v-if="columns[6].visible" />
              <el-table-column label="事件类型" align="center" key="type" prop="type" v-if="columns[7].visible" width="80" />
              <el-table-column label="数据来源" align="center" key="dataSrc" prop="dataSrc" v-if="columns[8].visible" width="80" :formatter="datasrcformat" />
              <el-table-column
                label="操作"
                align="center"
                width="160"
                class-name="small-padding fixed-width"
              >
                <template slot-scope="scope">
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-edit"
                    @click="handleUpdate(scope.row)"
                    v-hasPermi="['system:user:edit']"
                  >修改</el-button>
                  <el-button
                    v-if="scope.row.scid !== 1"
                    size="mini"
                    type="text"
                    icon="el-icon-delete"
                    @click="handleDelete(scope.row)"
                    v-hasPermi="['system:user:remove']"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <pagination
              v-show="total>0"
              :total="total"
              :page.sync="queryParams.pageNum"
              :limit.sync="queryParams.pageSize"
              @pagination="getList"
            />
          </el-row>
        </el-col>
      </el-row>

      <!-- 添加或修改参数配置对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="发震时刻" prop="otime">
                <el-input v-model="form.otime" readonly="readonly" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="事件ID" prop="scid">
                <el-input v-model="form.scid" readonly="readonly" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="纬度" prop="lat">
                <el-input v-model="form.lat" readonly="readonly" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="经度" prop="lon">
                <el-input v-model="form.lon" readonly="readonly" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="深度" prop="depth">
                <el-input v-model="form.depth" maxlength="30" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="震级" prop="m">
                <el-input v-model="form.m"  maxlength="20" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="参考地名" prop="locationCname">
                <el-input v-model="form.locationCname" maxlength="30" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="事件类型" prop="type">
                <el-input v-model="form.type"  maxlength="20" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
</template>

<script>
  import { listCatalog,getOneCatalog,updateCatalog,addCatalog } from "@/api/catalog/eqlist";

  export default {
    name: "List",
    data(){
      return{
        // 遮罩层
        loading: true,
        // 地震事件表格数据
        catalogList: null,
        // 日期范围
        dateRange: [],
        // 是否显示弹出层
        open: false,
        // 表单参数
        form: {},
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          orderByColumn:"otime",
          isAsc:"desc",
        },
        // 状态数据字典
        statusOptions: [],
        // 列信息
        columns: [
          { key: 0, label: `发震时刻`, visible: true },
          { key: 1, label: `事件ID`, visible: true },
          { key: 2, label: `纬度`, visible: true },
          { key: 3, label: `经度`, visible: true },
          { key: 4, label: `深度`, visible: true },
          { key: 5, label: `震级`, visible: true },
          { key: 6, label: `参考地名`, visible: true },
          { key: 7, label: `事件类型`, visible: true },
          { key: 8, label: `数据来源`, visible: true },
        ],
        // 表单校验
        rules: {
          type: [
            { required: true, message: "事件类型不能为空", trigger: "blur" }
          ],
          depth: [
            { required: true, message: "深度不能为空", trigger: "blur" }
          ],
          m: [
            { required: true, message: "震级不能为空", trigger: "blur" },
          ],
        },
        // 总条数
        total: 0,
        // 弹出层标题
        title: "",
      }
    },
    created() {
      this.getList();
    },
    methods:{
      /** 查询地震列表 */
      getList() {
        this.loading = true;
        listCatalog(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
            this.catalogList = response.rows;
            this.total = response.total;
            this.loading = false;
          }
        );
      },
      datasrcformat(row,column){
        switch (row.dataSrc) {
          case "sheng":
            return "山东速报";
            break;
          case "guo":
            return "国家速报";
            break;
        }
      },
      fun(val){
        return Number(val).toFixed(2);
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.dateRange = [];
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const catalogId = row.scid;
        getOneCatalog(catalogId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改地震信息";
          this.form.password = "";
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const userIds = row.userId || this.ids;
        this.$confirm('是否确认删除用户编号为"' + userIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delUser(userIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.scid != undefined) {
              updateCatalog(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addCatalog(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          otime: undefined,
          scid: undefined,
          lat: undefined,
          lon: undefined,
          depth: undefined,
          m: undefined,
          locationCname: undefined,
          type: undefined,
          dataSrc: undefined,
        };
        this.resetForm("form");
      },
      }
    }
</script>

